package com.bjpowernode.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@Controller
public class HelloController {

    @RequestMapping("/hello.html")
    public String hello() {
        System.out.println("执行处理程序！");
        //return "/WEB-INF/jsp/success.jsp";
        return "success"; // 配置视图前缀和后缀之后
    }

    //@RequestMapping("/static/**") // 以“/static/”下的所有资源
    public void xxx(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String uri = request.getRequestURI(); // 访问的资源路径
        System.out.println(uri);
        // 当前项目对象
        ServletContext context = request.getServletContext();
        String filename = context.getRealPath(uri);
        System.out.println(filename);
        FileInputStream in = new FileInputStream(filename);
        ServletOutputStream out = response.getOutputStream();

        byte[] bytes = new byte[1024];
        int len;
        while ((len=in.read(bytes))!=-1) {
            out.write(bytes,0,len);
        }
        out.close();
        in.close();
    }
}
